<?php

/**
 * 附件管理
 * File Name：AttachmentController.php
 * File Encoding：UTF-8
 * File New Time：2014-8-21 14:36:06
 * Author：水平凡
 * Mailbox：admin@abc3210.com
 */
class AttachmentController extends AdminBase {

    //附件列表
    public function actionIndex() {
        //搜索
        if (IS_POST) {
            foreach ($_POST as $k => $v) {
                if ($v == '') {
                    unset($_POST[$k]);
                }
            }
            $this->redirect(self::U(Yii::app()->getController()->getAction()->id, $_POST));
            exit;
        }
        $where = array(
            'order' => 'aid desc',
        );
        $model = AttachmentModel::model();
        //上传时间
        $start_uploadtime = $_GET['start_uploadtime'];
        $end_uploadtime = $_GET['end_uploadtime'];
        if ($start_uploadtime) {
            $this->assign('start_uploadtime', $start_uploadtime);
            $where['uploadtime'] = array('GT', strtotime($start_uploadtime));
            if ($end_uploadtime) {
                $this->assign('end_uploadtime', $end_uploadtime);
                $where['uploadtime'] = array(array('GT', strtotime($start_uploadtime)), array('LT', strtotime($start_uploadtime)), 'AND');
            }
        }
        //附件类型
        $fileext = trim($_GET['fileext']);
        if ($fileext) {
            $where['fileext'] = $fileext;
            $this->assign('fileext', $fileext);
        }
        $filename = trim($_GET['filename']);
        if ($filename) {
            $where['filename'] = array('LIKE', "%{$filename}%");
            $this->assign('filename', $filename);
        }

        $this->basePage($model, $where, 20);
        $this->render();
    }

    //删除附件
    public function actionDelete() {
        $model = AttachmentModel::model();
        if (IS_POST) {
            $aid = $_POST['aid'];
        } else {
            $aid = (int) $_GET['aid'];
        }
        if ($model->deleteFileByAid($aid)) {
            $this->success('删除成功！');
        } else {
            $this->error('删除失败！');
        }
    }

    //上传
    public function actionUpload() {
        if (IS_POST) {
            if (UploadFile::getInstance()->upload()) {
                $this->success('上传成功！', self::U('index'));
            } else {
                $this->error(UploadFile::getInstance()->getError() ? UploadFile::getInstance()->getError() : '上传失败！');
            }
        } else {
            $this->render();
        }
    }

}
